// 1001221207000001633
const basePage = require('../../../common/BasePage')
Page({
  behaviors: [basePage],

  /**
   * 页面的初始数据
   */
  data: {
    showCoupon: false,
    order: {},
    couponArr: [],
    storeSid: '',
    coupon: {
      reduceAmount: 0
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.storeSid = options.storeSid;
    let order = wx.getStorageSync('order');
    order.thumb = this.get_imgUrl(order.avatar);
    let total = 0;
    order.goodsList.map(val => {
      val.thumb = this.get_imgUrl(val.goodsImg);
      total += val.goodsPrice * val.goodsNum
    })
    order.total = total
    this.setData({
      // user: user,
      // itemArr: order.goodsList,
      order: order
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  coupon_click() {
    this.request({
      url: this.data.host + "/v1/coupons/store-user",
      data: {
        size: 50,
        used: 1,
        userSid: this.data.order.userSid
      },
      loadingTip: "加载..."
    }, res => {
      this.setData({
        couponArr: res.data.records,
        showCoupon: true
      })
    })
  },
  scan_bar() {
    wx.scanCode({
      onlyFromCamera: true,
      scanType: ["qrCode", "barCode"],
      success: (result) => {

        if (result.result) {
          this.request({
            url: this.data.host + '/v1/orders/' + result.result + '/member-scan/' + this.data.storeSid,
            // url: this.data.host + '/v1/orders/' + 6901028001618 + '/member-scan/' + this.data.storeSid,
            method: "post",

          }, res => {
            res.data.thumb = this.get_imgUrl(res.data.image);
            res.data.goodsNum = 1;
            this.data.itemArr.push(res.data);
            this.setData({
              itemArr: this.data.itemArr
            })
          })
        }
      },
      fail: (res) => {
        wx.showToast({
          title: res.errMsg,
          icon: 'error'
        })
      },

    })

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  coupon_change(e) {
    // console.log('coupon_change',e.);
    this.setData({
      showCoupon: false,
      coupon: e.currentTarget.dataset.item
    })
    // this.req_pay(e.currentTarget.dataset.item.sid)
  },
  submit_click() {
    this.request({
      url: this.data.host + (this.data.order.userSid ? "/v1/orders/member-settle" : "/v1/orders/goods-settle"),
      data: {
        goodsOrderSn: this.data.order.goodsOrderSn,
        storeSid: this.data.storeSid,
        userSid: this.data.order.userSid,
        userCouponSid: this.data.coupon.sid
      },
      method: "post",
      loadingTip: "结算..."
      // header: {
      //   'content-type': 'application/x-www-form-urlencoded',
      // }
    }, res => {
      // console.log('拉取支付', res);
      if (this.data.order.userSid) {
        setTimeout(() => {
          wx.showToast({
            title: '下单成功',
          })
        }, 100);

        setTimeout(() => {
          wx.redirectTo({
            url: '/pages/pay/Code?sn=' + this.data.order.goodsOrderSn + "&storeSid=" + this.data.storeSid,
          })
        }, 2000);

      } else {
        wx.setStorageSync('sk_img', res.data)

        wx.redirectTo({
          url: '/pages/pay/Code?storeSid=' + this.data.storeSid + "&goodsOrderSn=" + this.data.order.goodsOrderSn
        })
      }

    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },


})